var G = {};
;(function(G){
	var Login = {};
	var Register = {};
	var screenWidth = 0;
	var screenHeight = 0;
	var hasBindevent = false;
	var bgColor = "#000";
	var opacity = 0.4;
	var loginPop;
	var mask;
	var main;
	var defaults = {
		bgColor : "#000",
		opacity : 0.4,
		beforeOpen : function(){},
		afterOpen : function(){},
		beforeClose : function(){},
		afterClose : function(){}
	} 
	var or_password = "请输入原始密码";
	var password_new = "请输入新密码";
	var pwInpInt = "请输入6位数密码";
	var telInt = "请输入11位手机号";
	var password_new_t = $(".passwordInp_new").val();
	var mobReg = /1[3-8]+\d{9}/; //验证11位手机号
	Login.init = function(){
		screenWidth = $(window).width();
		screenHeight = $(window).height();
		loginPop = $("#gLoginPopW");
		mask = loginPop.find(".gloginMask");
		main = loginPop.find(".gloginMain");
		var slideUl = main.find(".loginSlideUl");
		var step = slideUl.children().first().width();
		main.on("click",".gloginPopClose",function(){
			Login.close();
		}).on("click",".loginW .forget_want",function(e){
			slideUl.animate({left:-step-60});
		}).on("click",".singupW .botW a.forget",function(e){
			slideUl.animate({left:-990}); 
		}).on("click",".forget .botW a.sloginBtn",function(e){
			slideUl.animate({left:0});
		}).on("click","#glogin_loginBtn",function(e){
			var userInp = $("#glogin_userInp").val();
			var passwordInp_new = $("#glogin_passwordInp_new").val(); 
			login(userInp,passwordInp_new);
			return false;
		}).on("keydown",function(e){
			var keyCode = e.keyCode;
			var target = $(e.target);
			if(target.hasClass("gloginInp")){
				target.parents(".glgInpCtrol").next(".errorTip").hide();
			}
			if(keyCode == 13){
				var userInp = $("#glogin_userInp").val();
				var passwordInp_new = $("#glogin_passwordInp_new").val(); 
				login(userInp,passwordInp_new);
			}
			if(keyCode == 9 && !target.hasClass("tabInp")){
				return false;
			}
		}).on("blur","input.varifyInp",function(e){
			var t = $(this);
		}).on("blur","input#passwordInp_new",function(e){
			var password = $(this).val();
			var p =password.length;
			if(p<6){
				
				$(".password_t").css("display","inline-block");
			}
			else{
				$(".password_t").css("display","none");
			}
		}).on("blur","input#passwordInp_ag",function(e){
			var password_ag = $(this).val();
			var password = $("#passwordInp_new").val();
			//alert(password_ag)
			if(password_ag!=password){
				$(".password_w").css("display","inline-block");
			}
			else{
				$(".password_w").css("display","none");
			}
		}).on("blur","input#glogin_telInp",function(e){
			var t = $(this);
			if(!t.val()){
				if(t.hasClass("telInp")){
					t.val(telInt)
				}
			}
		}).on("focus","input#glogin_telInp",function(e){
			var t = $(this);
			var tv = t.val();
			if(t.hasClass("telInp")){
				t.val("")
			}
		}).on("click","#glogin_getmaCode",function(e){
			getmaCode();
			return false;
		}).on("click","#glogin_singupBtn",function(e){
			singin();
			return false;
		}).on("click","#glogin_password",function(e){
			password();
			return false;
		})
		main.css({
			top : -main.height()-10,
			left : (screenWidth-main.width()) / 2
		})
		//点击弹出登录框
		$("#loginStart").on("click",function(e){
			Login.open();
			return false;
		})
		//点击退出登录
		$("#gToplogout").on("click",function(e){
			$.ajax({
				"url":"../call/handle.php",
				"data":{"from":"logout"},
				"dataType":"json",
				"success":function(data){
					window.location.reload();
					window.location.href="http://yd.12301.cc/";
					//window.location.href="http://mx.12301.cc/client/home.html";
				},
				"error":function(){
					window.location.href="http://yd.12301.cc/";
					//window.location.href="http://mx.12301.cc/client/home.html";
				}
			});
			return false;
		})
		
		var userInp = $("#glogin_userInp");
		var telInp = $("#glogin_telInp");
		userInp.val(or_password);
		telInp.val(telInt);
	}
	Login.open = function(config){
		config = config || {};
		for(var i in defaults){
			if(typeof config[i] === "undefined"){
				config[i] = defaults[i];
			}
		}
		config.beforeOpen();
		refresh();
		mask.css({zIndex:10000,backgroundColor:config.bgColor,opacity:config.opacity}).fadeIn(function(){
			main.show().animate({top:(screenHeight-main.height())/2},400,function(){
				 config.afterOpen();
			})
		});
	}
	Login.close = function(config){
		config = config || {};
		for(var i in defaults){
			if(typeof config[i] === "undefined"){
				config[i] = defaults[i];
			}
		}
		config.beforeClose();
		main.animate({top:-main.height()-10},400,function(){
			config.afterClose();
		})
		mask.fadeOut();
	}
	//修改密码
	function login(userInp,passwordInp_new,json){
		var loginBtn = $("#glogin_loginBtn");
		json = json || {};
		var success = json.success || function(){}; //成功
		$.ajax({
                        "url":"http://www.12301.cc/admin/pppp.php",
                        "dataType":"json",
                        "type": 'POST',
                        "data": {
								 action : "login_t",
								 username : userInp,
								 password : passwordInp_new
							},
                        "success":function(data){
                            if(data.status=='ok'){
                                alert(data.msg);
                                window.location=window.location.href;
                            }
                            else if (data.status=='fail') {
                                alert(data.msg);
                            }
                        }
                    });
	}
	//找回密码
	function singin(){
		loginPop = $("#gLoginPopW");
		mask = loginPop.find(".gloginMask");
		main = loginPop.find(".gloginMain");
		var slideUl = main.find(".loginSlideUl");
		var code = $("#glogin_macodeInp").val();
		var mobile = $("#glogin_telInp").val();
		var signBtn = $("#glogin_singupBtn");
		if(signBtn.hasClass("loading")) return false;
		code = $.trim(code);
		mobile = $.trim(mobile);
		if(!mobile){
			alert("*请输入手机号");
			return false;
		}
		if(mobile.length !== 11 || !mobReg.test(mobile)){
			alert("*请输入有效手机号");
			return false;
		}
		if(!code){
			alert("请输入验证码");
			return false;
		}
		$("#glogin_singupBtn").addClass("loading");
		$.ajax({
			url : "../register.php",
			type : "POST",
			dataType : "JSON",
			data : {
				action : "register",
				mobile : mobile,
				valiCode : code
			},
			success : function(res){
				if(res && res.code){
					if(res.code == 100){ //成功
						alert("注册成功！");
						window.location.reload();
					}else if(res.code == 101){ //帐号已注册
						alert("此帐号已被注册");
					}else if(res.code==102){
						res.msg && alert(res.msg);
					}else if(res.code == 103){ //验证码不正确
						alert("验证码不正确");
					}else{
						alert("注册失败，请稍后重试");
					}
				}else{ //出错
					alert("注册失败，请稍后重试");
					
				}
			},
			error : function(){
				//alert("注册失败，请稍后重试");
				slideUl.animate({left:-990}); 
			},
			complete : function(){
				signBtn.removeClass("loading");
			}
		})
	}
	//获取验证码
	function getmaCode(){
		var getBtn = $("#glogin_getmaCode");
		var telInp = $("#glogin_telInp");
		var tel = $.trim(telInp.val());
		if(getBtn.hasClass("loading")) return false;
		var timer = null;
		var time = 60;
		if(!tel){
			alert("*请输入手机号");
			return;
		}
		if(tel.length !== 11 || !mobReg.test(tel)){
			alert("*请输入有效手机号");
			return;
		}
		getBtn.addClass("loading").val("正在获取...");
		$.ajax({
			url : "../register.php",
			type : "POST",
			dataType : "JSON",
			data : {
				action : "sendCode",
				mobile : tel
			},
			success : function(res){
				if(res && res.code){
					if(res.code == 100){ //成功
						_reGet();
					}else if(res.code == 102){ //手机号已被注册过
						alert("此手机号已被注册过");
						getBtn.val("获取验证码");
						getBtn.removeClass("loading");
					}else{ //失败
						_reGet();
						alert("获取验证码失败,60s后请重新获取");
					}
				}else{ //出错
					_reGet();
					alert("获取验证码时服务器出错，60s后请重新获取");
				}
			},
			error : function(xhr,text){ //出错
				_reGet();
				alert("获取验证码时服务器出错，60s后请重新获取");
			}
		})
		function _reGet(){
			timer = setInterval(function(){
				if(time==0){
					getBtn.val("获取验证码");
					getBtn.removeClass("loading");
					clearInterval(timer);
					return;
				}
				time--;
				getBtn.val(time+"秒后重新获取");
			},1000)
		}
	}
	function refresh(){
		main.find(".gloginInp").val("");
		$("#glogin_telInp").val(telInt);
		main.find(".errorTip").hide();
		main.find(".loginSlideUl").css({left:0})
	}
	
	function password(){
		sure_password = $("#sure_password").val();
		sure_password_again = $("#sure_password_again").val();
		$.ajax({
			url : "../register.php",
			type : "POST",
			dataType : "JSON",
			data : {
				action : "register",
				sure_password : sure_password,
				vsure_password_again : sure_password_again
			},
			success : function(res){
				if(res && res.code){
					if(res.code == 100){ //成功
						alert("注册成功！");
						window.location.reload();
					}else if(res.code == 101){ //帐号已注册
						alert("此帐号已被注册");
					}else if(res.code==102){
						res.msg && alert(res.msg);
					}else if(res.code == 103){ //验证码不正确
						alert("验证码不正确");
					}else{
						alert("注册失败，请稍后重试");
					}
				}else{ //出错
					alert("注册失败，请稍后重试");
					
				}
			},
			error : function(){
				//alert("注册失败，请稍后重试");
				slideUl.animate({left:-990}); 
			},
			complete : function(){
				signBtn.removeClass("loading");
			}
		})
	}
	
	G.Login = Login;
	$(function(){
		G.Login.init();
	
		
		
	})
	
})(G);